<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
	<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

	<%-- CSS Style Declarations --%>
	<link rel ="stylesheet" type="text/css" href="stylesheets/style.css" ></link>
	
	<%-- Java imports --%>
	<%@ page import="java.util.*" %>
	<%@ page import="flashcardtest.FlashCardSet" %>
	<%@ page import="flashcardtest.FlashCard" %>
	<%@ page import="flashcardtest.FlashCardCore" %>
	<%@ page import="com.google.appengine.api.users.UserService" %>
	<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
	<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
	<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
	<%@ page import="com.google.appengine.api.datastore.Entity" %>
	<%@ page import="com.google.appengine.api.datastore.PreparedQuery" %>
	<%@ page import="com.google.appengine.api.datastore.Query" %>
	
	<%-- Get reference to Java Objects --%>
	<% 	FlashCardCore core = (FlashCardCore) request.getSession().getAttribute("core");
		if (core == null) {
			core = new FlashCardCore();
			request.getSession().setAttribute("core", core);
		}
		%>
	<%-- Detect User Logged In --%>
	<%
		if (!core.loggedIn()){
			response.sendRedirect("/login.jsp");
		}
	  	%>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Edit Dictionary</title>
</head>

<body>

<div id="center-column">

<!-- Header -->
<div id="header">
<FORM action="tomain" method="post">
			<input
				type="submit"
				name="button1"
				class="headerbutton"
				value="Main Menu"
				title="">
</FORM>
</div> <!-- header -->

<!-- User name -->
<div id="user_name">
	<% out.println( core.getUserName() + 
			" (<a href=\"" + core.getLogoutURL(request) +
			"\">sign out</a>)"); %>
</div> <!-- /user name -->

<!-- Dictionary Title -->
<div id="set_name">
	<% out.println(core.getSetName());%>
</div> <!-- dictionary title -->

<!-- Page Title -->
<div id="page_title">
	Edit Dictionary - <% out.println(Integer.toString(core.getFlashCardSet().size()));%>
</div> <!-- page title -->

<!-- Contents Selector -->
<div style="
	width: 600px;
	text-align:left;
	position:absolute;
	top:140px;
	left: 20px;
	">
<FORM action="editdictionary" method="post">
<select multiple size="13" name="Dictionary Contents" style="width:18em">
<% 	
%>
<%
	ArrayList<String> contents = core.getSetContents();
	
	Iterator itr = contents.iterator();
	while (itr.hasNext()) {
		String entry = (String)itr.next();
		out.println("<option value=\"" + entry + "\">" + entry + "</option>" );
	}
%>
	</select><p>
	<input type="submit" name="mode" value="Delete Entry">
	<input type="submit" name="mode" value="Order by Key 1">
	<input type="submit" name="mode" value="Order by Key 2">
	<input type="submit" name="mode" value="Delete Dictionary">
</FORM>

</div> <!-- contents selector -->


<!-- New Word Form -->
<form action="editdictionary" method="post">


<div style="
	position:absolute;
	top:140px;
	left:280px;
	width:300px;
	text-align:left;
	">
	<fieldset>
	<legend>New Word</legend>
	
	<% out.println(core.getFlashCardSet().getCategory1() + ":");%>
	<input type="text" class="text" size="20" maxlength="20" name="key1" /><p>
	<% out.println(core.getFlashCardSet().getCategory2() + ":");%>
	<input type="text" class="text" size="20" maxlength="20" name="key2" /><p>
	<input type="submit" name="mode" value="Add Word" />
	
	</fieldset>
</div>	

</form> <!-- new word form -->

<!-- Message -->
<div style="
	position:absolute;
	top: 120px;
	width: 600px;
	text-align:center;
	">
	<% out.println(request.getSession().getAttribute("message")); %>
</div> <!-- message -->

</div> <!-- center-column -->
</body>
</html>